﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbSharp.Framework.Driver;
using System.Data.Common;
using System.Globalization;

namespace DbSharp.Framework.ReleaseManage
{
    public class EditionFuncControlOption
    {
        #region Private members

        private DateTime m_FixedExpiredDate;
        private bool m_NeedComputeExpiration;
        private IDateTimeService m_DateTimeService;
        private ILicenseAuth m_LicenseAuthService;
        private IEvaluationTrackService m_EvaluationTrack;

        #endregion

        #region Private methods

        private bool IsLaterThanExpiredDate(DateTime currentDate)
        {
            if (currentDate.Date.CompareTo(m_FixedExpiredDate) >= 0)
                return true;
            else
                return false;
        }

        #endregion


        public EditionFuncControlOption(DateTime fixedExpiredDate, bool needComputeExpiration,
            IDateTimeService dateTimeService, ILicenseAuth licenseAuthService, IEvaluationTrackService evaluationTrack)
        {
            m_FixedExpiredDate = fixedExpiredDate;
            m_NeedComputeExpiration = needComputeExpiration;
            m_DateTimeService = dateTimeService;
            m_LicenseAuthService = licenseAuthService;
            m_LicenseAuthService.RetrieveLicenseData();
            m_EvaluationTrack = evaluationTrack;
        }

        #region Public property/method

        public bool AdvancedDiffEnabled { get; set; }
 
        public bool TestInSandboxEnabled { get; set; }

        public bool UnitTestSetupEnabled { get; set; }

        public int MaxDbConnConfigCount { get; set; }

        public int MaxPerfTestCaseCount { get; set; }

        public int MaxUnitTestCaseCount { get; set; }

        public bool IsExpired(ITestDataSource testDb, DbConnection dbConn)
        {
            if (m_NeedComputeExpiration == false)
                return false;
            else
            {
                if (m_EvaluationTrack.GetEvaluationTrackInfo().Expired)
                    return true;
                else
                {
                    if (testDb == null)
                    {
                        //query webservice and local time
                        DateTime serviceDate = m_DateTimeService.GetCurrentTime();
                        return IsLaterThanExpiredDate(serviceDate);
                    }
                    else
                    {
                        //query database server time
                        DateTime serverDate = testDb.GetDbServerTime(dbConn);
                        return IsLaterThanExpiredDate(serverDate.Date);
                    }
                }
            }
        }

        public void SetEvaluationExpired()
        {
            if (m_EvaluationTrack != null)
            {
                var trackInfo = m_EvaluationTrack.GetEvaluationTrackInfo();
                trackInfo.Expired = true;
                m_EvaluationTrack.TryToSetEvaluationTrackInfo(trackInfo);
            }
        }

        public bool IsValidLicense()
        {
            return m_LicenseAuthService.Authenticate();
        }

        public bool IsDbConnCfgCountOverflow(int connectionCfgCount)
        {
            if (MaxDbConnConfigCount <= 0)
                return false;
            else
            {
                if (connectionCfgCount > MaxDbConnConfigCount)
                    return true;
                else
                    return false; 
            }
        }

        public bool IsPerfCaseCountOverflow(int perfCaseCount)
        {
            if (MaxPerfTestCaseCount <= 0)
                return false;
            else
            {
                if (perfCaseCount > MaxPerfTestCaseCount)
                    return true;
                else
                    return false;
            }
        }

        public bool IsUnitCaseCountOverflow(int unitCaseCount)
        {
            if (MaxUnitTestCaseCount <= 0)
                return false;
            else
            {
                if (unitCaseCount > MaxUnitTestCaseCount)
                    return true;
                else
                    return false;
            }
        }
        #endregion
    }

    public static class EditionPresentOption
    {
        public static string GetEditionName()
        {
            string result;
            result = "Community Edition";
 
            return result;
        }

    }

    public static class EditionOptionFactory
    {
        public static EditionFuncControlOption CreateDefaultEditionOption()
        {
            EditionFuncControlOption result;
            result = CreateProfEditionOption();
 
            return result;
        }

        public static EditionFuncControlOption CreateProfEditionOption()
        {
            DateTime expiredDate = DateTime.UtcNow.AddYears(1);
            bool needComputeExpiration = false;
            IDateTimeService dateService = new LocalMachineDateService();
            ILicenseAuth licenseAuthService = new NullLicenseAuth();
            IEvaluationTrackService evaluationTrack = null;

            var editionOption = new EditionFuncControlOption(expiredDate, needComputeExpiration, dateService, licenseAuthService, evaluationTrack);
            editionOption.AdvancedDiffEnabled = true;
            editionOption.TestInSandboxEnabled = true;
            editionOption.MaxDbConnConfigCount = -1;
            editionOption.UnitTestSetupEnabled = true;
            editionOption.MaxPerfTestCaseCount = -1;
            editionOption.MaxUnitTestCaseCount = -1;

            return editionOption;
        }


       
        public static EditionFuncControlOption CreateExpressEditionOption()
        {
            return CreateProfEditionOption();  
        }


        public static EditionFuncControlOption CreateTrialEditionOption(IEvaluationTrackService evaluationTrack)
        {
            return CreateProfEditionOption();   
        }

         
    }
}
